// 通用接口
const mongoose = require('mongoose');
const User = mongoose.model("users");
const open_api = require('../controller/open_api');

// 玩家锻炼
exports.UserGoGym = async (req, res) => {
    let name = req.body.user;
    let gymnum = req.body.gymnum;
    let gymtype = req.body.gymtype;
    let myInfo;
    if (!name) {
        return res.json({ code: 0, msg: "name error", data: null })
    }
    if (name && name.length > 10) {
        myInfo = await User.findById(uid)
    } else {
        myInfo = await User.findOne({ name: name });
        // 暂时锻炼无消耗，先计算
        if (gymtype) {
            if (gymnum) {
                if (gymtype == 'strength') {
                    up_num = Math.floor(myInfo.strength + gymnum);
                    const updates = { $set: { strength: up_num } };
                    await User.updateOne({ name: name }, updates, function (error) {
                        if (error) {
                            console.log(error);
                        } else {
                            console.log("锻炼力量成功");
                        }
                    });
                };
                if (gymtype == 'defense') {
                    up_num = Math.floor(myInfo.defense + gymnum);
                    const updates = { $set: { defense: up_num } };
                    await User.updateOne({ name: name }, updates, function (error) {
                        if (error) {
                            console.log(error);
                        } else {
                            console.log("锻炼体质成功");
                        }
                    });
                };
                if (gymtype == 'magic') {
                    up_num = Math.floor(myInfo.magic + gymnum);
                    const updates = { $set: { magic: up_num } };
                    await User.updateOne({ name: name }, updates, function (error) {
                        if (error) {
                            console.log(error);
                        } else {
                            console.log("锻炼灵气成功");
                        }
                    });
                };
                if (gymtype == 'speed') {
                    up_num = Math.floor(myInfo.speed + gymnum);
                    const updates = { $set: { speed: up_num } };
                    await User.updateOne({ name: name }, updates, function (error) {
                        if (error) {
                            console.log(error);
                        } else {
                            console.log("锻炼速度成功");
                        }
                    });
                };
            };
        };
    };
    open_api.getUserGymInfo(req, res);
}



